<?php

namespace Home\Controller;

class InfoController extends BaseController {

    public function _initialize() {
        //重写，勿动
    }

    public function getList() {
        $p = I('p', 0, 'intval');

        if ($p) {
            $page = $p * 10;
        }
        $where['is_del'] = 0;

        $list = M('Information')
                ->field('info_id,title,img_url,view_num,create_time')
                ->where($where)
                ->order('sort')
                ->limit($page, 10)
                ->select();

        if ($list) {
            foreach ($list as &$val) {
                if (!empty($val['img_url'])) {
                    $val['img_url'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val['img_url'];
                }
            }
            unset($val);
        } else {
            $list = [];
        }

        $this->success('获取成功', $list);
    }

    public function detail() {
        $id = I('id', 0, 'intval');
        if ($id == 0) {
            $this->error('错误的ID');
        }

        $data = M('Information')->where(['info_id' => $id, 'is_del' => 0])->find();

        if ($data) {
            M('Information')->where(['info_id' => $id])->setInc('view_num');
        }

        $this->assign('data', $data);
        $this->display();
    }

}
